package cn.good.yan.sf.t6;

import java.util.Iterator;

/**
 * 6 双端队列 - 基于 链表实现的队列
 *
 * @author shengren.yan
 * @create 2024-08-03
 */
public class LinkListDQueue<E> implements DQueue<E>, Iterable<E> {

    static class Node<E> {
        Node<E> prev;
        E value;
        Node<E> next;

        public Node(Node<E> prev, E value, Node<E> next) {
            this.prev = prev;
            this.value = value;
            this.next = next;
        }
    }

    Node<E> head = new Node<>(null, null, null);
    private int size;
    private int num;

    @SuppressWarnings("all")
    public LinkListDQueue(int num) {
        this.num = num;
        head.next = head;
        head.prev = head;
    }

    @Override
    public boolean offerFirst(E e) {
        if (isFull()) {
            return false;
        }
        Node<E> a = head;
        Node<E> b = head.next;
        Node<E> node = new Node<>(a, e, b);
        a.next = node;
        b.prev = node;
        size++;
        return true;
    }

    @Override
    public boolean offerLast(E e) {
        if (isFull()) {
            return false;
        }
        Node<E> a = head.prev;
        Node<E> b = head;
        Node<E> node = new Node<>(a, e, b);
        a.next = node;
        b.prev = node;
        size++;
        return true;
    }

    @Override
    public E poolFirst() {
        if (isEmpty()) {
            return null;
        }
        Node<E> node = head.next;
        head.next = node.next;
        node.next.prev = head;
        size--;
        return node.value;
    }

    @Override
    public E poolLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> node = head.prev;
        head.prev = node.prev;
        node.prev.next = head;
        size--;
        return node.value;
    }

    @Override
    public E peekFirst() {
        if (isEmpty()) {
            return null;
        }
        Node<E> node = head.next;
        return node.value;
    }

    @Override
    public E peekLast() {
        if (isEmpty()) {
            return null;
        }
        Node<E> node = head.prev;
        return node.value;
    }

    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    @Override
    public boolean isFull() {
        return size == num;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<E>() {
            Node<E> n = head.next;

            @Override
            public boolean hasNext() {
                return n != head;
            }

            @Override
            public E next() {
                E value = n.value;
                n = n.next;
                return value;
            }
        };
    }

    public static void main(String[] args) {
        LinkListDQueue<Integer> q = new LinkListDQueue<>(5);
        System.out.println("移除头元素：" + q.poolFirst());
        System.out.println("移除尾元素：" + q.poolLast());
        System.out.println("获取头元素：" + q.peekFirst());
        System.out.println("获取尾元素：" + q.peekLast());
        System.out.println("添加头元素：" + q.offerFirst(1));   // 添加头元素  1
        System.out.println("添加头元素：" + q.offerFirst(2));   // 添加头元素  2 1
        System.out.println("添加头元素：" + q.offerFirst(3));   // 添加头元素  3 2 1
        System.out.println("添加头元素：" + q.offerFirst(4));
        System.out.println("添加头元素：" + q.offerFirst(5));
        System.out.println("添加头元素：" + q.offerFirst(6));
        System.out.println("添加头元素：" + q.offerFirst(7));
        System.out.println("--------------");
        for (Integer i : q) {
            System.out.println(i);
        }
        System.out.println("--------------");
        System.out.println("获取头元素：" + q.peekFirst());
        System.out.println("获取尾元素：" + q.peekLast());
        System.out.println("移除头元素：" + q.poolFirst());
        System.out.println("移除头元素：" + q.poolFirst());
        System.out.println("移除尾元素：" + q.poolLast());
        System.out.println("移除尾元素：" + q.poolLast());
        System.out.println("获取头元素：" + q.peekFirst());
        System.out.println("获取尾元素：" + q.peekLast());
        System.out.println("--------------");
        for (Integer i : q) {
            System.out.println(i);
        }
        System.out.println("--------------");
        System.out.println("添加尾元素：" + q.offerLast(20));   // 尾部添加  3 20
        System.out.println("添加尾元素：" + q.offerLast(30));   // 尾部添加  3 20 30
        System.out.println("添加尾元素：" + q.offerLast(40));
        System.out.println("获取头元素：" + q.peekFirst());
        System.out.println("获取尾元素：" + q.peekLast());
        System.out.println("移除头元素：" + q.poolFirst());
        System.out.println("移除尾元素：" + q.poolLast());
        System.out.println("--------------");
        for (Integer i : q) {
            System.out.println(i);
        }
        System.out.println("--------------");

    }

}
